package com.example.myapplication.untils;

import android.content.Context;
import android.icu.text.DecimalFormat;
import android.location.Location;
import android.location.LocationManager;

public class LocationUtils {

    private static final int TWO_MINUTES = 1000 * 60 * 2;

    /**
     * 判断是否为最新位置信息
     *
     * @param location        当前的位置信息
     * @param lastKnownLocation 上一次获取的位置信息
     * @return 是否为最新位置信息
     */
    public static boolean isBetterLocation(Location location, Location lastKnownLocation) {
        if (lastKnownLocation == null) {
            return true;
        }

        long timeDelta = location.getTime() - lastKnownLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
        boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
        boolean isNewer = timeDelta > 0;

        if (isSignificantlyNewer) {
            return true;
        } else if (isSignificantlyOlder) {
            return false;
        }

        int accuracyDelta = (int) (location.getAccuracy() - lastKnownLocation.getAccuracy());
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;

        boolean isFromSameProvider = isSameProvider(location.getProvider(),
                lastKnownLocation.getProvider());

        if (isMoreAccurate) {
            return true;
        } else if (isNewer && !isLessAccurate) {
            return true;
        } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
            return true;
        }

        return false;
    }

    /**
     * 判断两个提供者是否相同
     *
     * @param provider1 提供者1
     * @param provider2 提供者2
     * @return 是否相同
     */
    public static boolean isSameProvider(String provider1, String provider2) {
        if (provider1 == null) {
            return provider2 == null;
        }
        return provider1.equals(provider2);
    }

    /**
     * 根据给定的 LocationProvider 获取 Location
     *
     * @param context          上下文
     * @param locationProvider LocationProvider
     * @return Location 对象
     */
    public static Location getLocationByProvider(Context context, String locationProvider) {
        Location location = null;
        LocationManager locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        try {
            if (locationManager.isProviderEnabled(locationProvider)) {
                location = locationManager.getLastKnownLocation(locationProvider);
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        return location;
    }

    /**
     * 将 Location 转换为经纬度字符串
     *
     * @param location Location 对象
     * @return 经纬度字符串，格式为 "latitude,longitude"
     */
    public static String formatLocationToString(Location location) {
        if (location == null) {
            return "0.000000,0.000000";
        }
        DecimalFormat decimalFormat = new DecimalFormat("#.######");
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        String latStr = decimalFormat.format(latitude);
        String lngStr = decimalFormat.format(longitude);
        return latStr + "," + lngStr;
    }

}
